#include "pch.h"
#include <string>
#include <iostream>
#include <time.h>
using namespace std;


//产生一定范围内随机数
int random(int x)
{
	if (x == 0)
	{
		return 0;
	}
	else
	{
		return rand() % x;
	}
}

//素数
const int allPrime[16] = { 1,2,3,5,7,11,13,17,19,23,29,31,37,41 ,43,47 };

//题目类
class question
{
private:
	int findDivNum(int num1)
	{
		int rand;
		do
		{
			rand = random(15);
		} 
		while (num1%allPrime[rand] != 0);

		return allPrime[rand];
	}

public:
	//构造函数，随机产生一个题目
	question()
	{
		//两个操作数都在50以内
		this->num1 = random(50);
		int rand = random(4);
		//随机确定是加法还是减法
		if (rand==0)
		{
			//加法
			this->uni = 0;
			//两个操作数的和不超过100
			int d = 50 - this->num1;
			//产生第二个操作数
			this->num2 = random(d);
			//存储结果
			this->result = this->num1 + this->num2;
		}
		else if(rand==1)
		{
			//减法
			this->uni = 1;
			//两个操作数的差不是负数
			this->num2 = random(this->num1);
			//存储结果
			this->result = this->num1 - this->num2;
		}
		else if (rand == 2)
		{
			//乘法
			this->uni = 2;
			this->num2 = random(10);
			//存储结果
			this->result = this->num1 * this->num2;
		}
		else
		{
			//除法
			this->uni = 3;
			this->num2 = this->findDivNum(num1);
			//存储结果
			this->result = this->num1 / this->num2;
		}
	}


	//操作数1
	int num1;
	//操作数2
	int num2;
	//加法还是减法
	int uni;
	//正确结果
	int result;


	//将题目显示出来
	void display()
	{
		cout << num1;
		if (uni == 0)
		{
			cout << "+";
		}
		else if (uni == 1)
		{
			cout << "-";
		}
		else if (uni == 2)
		{
			cout << "*";
		}
		else if (uni == 3)
		{
			cout << "/";
		}
		cout << num2 << "=";
	}


	//判定答案是否正确
	bool isRight(int input)
	{
		return input == result;
	}
};


class student
{
public:
	student()
	{
		score = 0;
	}

	//分数
	int score;
	//考生姓名
	string name;
};


class exam
{
private:
	//题目数量
	static int questionNum;
	//允许回答次数
	static int reanswer;
	//考生
	static student stu;


	//根据评分返回等级
	static string getLevel(int score)
	{
		if (score > 90)
		{
			return "SMART";
		}
		else if (score > 80 && score <= 90)
		{
			return "GOOD";
		}
		else if (score > 70 && score <= 80)
		{
			return "OK";
		}
		else if (score > 60 && score <= 70)
		{
			return "PASS";
		}
		else
		{
			return "FAIL";
		}
	}


	//计算百分制得分
	static int caluScore(int score)
	{
		return score * 10 / (int)questionNum;
	}


public:
	//设定系统参数
	static void setPar()
	{
		while (1)
		{
			cout << "请输入题目数量：";
			cin >> questionNum;
			if (questionNum > 0)
			{
				break;
			}
			else
			{
				cout << "题目数量必须大于0！" << endl;
			}
		}

		while (1)
		{
			cout << "请输入允许回答次数：";
			cin >> reanswer;
			if (reanswer > 0)
			{
				break;
			}
			else
			{
				cout << "允许回答次数必须大于0！" << endl;
			}
		}
	}


	//准备考试
	static void ready()
	{
		exam::stu = student();
		//初始化分数
		stu.score = 0;

		//输入考生姓名
		cout << "--------考试准备开始----------" << endl;
		cout << "请输入考生姓名：";
		cin >> stu.name;
	}

	
	//考试开始
	static void start()
	{
		//考试开始
		cout << "----------考试开始-----------" << endl;
		for (int i = 0; i < questionNum; i++)
		{
			//构造题目
			question q;
			bool isRight = false;
			for (int j = 0; j < reanswer; j++)
			{
				//展示题目
				q.display();
				//用户输入答案
				int input;
				cin >> input;
				//判断对错
				if (q.isRight(input))
				{
					//根据回答次数不同加不同的分
					if (j <= reanswer / 3)
					{
						stu.score += 10;
						isRight = true;
						break;
					}
					else if (j <= reanswer * 2 / 3)
					{
						stu.score += 7;
						isRight = true;
						break;
					}
					else
					{
						stu.score += 5;
						isRight = true;
						break;
					}
				}
				else
				{
					if (j != reanswer - 1)
					{
						cout << "回答错误，请重新作答" << endl;
					}
					else
					{
						cout << "回答错误" << endl;
					}
				}
			}

			//如果没有回答正确，显示出正确答案
			if (!isRight)
			{
				cout << "正确答案为" << q.result << endl;
			}
		}
	}


	//考试结束
	static void finish()
	{
		cout << "----------考试结束-----------" << endl;
		//计算百分制得分
		stu.score = caluScore(stu.score);
		//输出成绩信息
		cout << " 考生：" << stu.name;
		cout << " 分数：" << stu.score;
		cout << " 评级：" << getLevel(stu.score) << endl;
	}
};
int exam::questionNum;
int exam::reanswer;
student exam::stu;


int main()
{
	//设定随机种子
	srand((int)time(0));
	exam::setPar();

	while (1)
	{
		exam::ready();
		exam::start();
		exam::finish();

		//询问是否继续
		cout << "是否继续考试？(Y/N)";
		string choose;
		cin >> choose;
		if (choose == "N")
		{
			break;
		}
	}
}